﻿//add by liu
function modaldialogclose(txtclientId, valclientId, txt, val, buttonId, setfatherID, fatherText) {
    debugger;
    if (window != null) {
        var obj = window.dialogArguments;
        var textbox1 = obj.document.getElementById(txtclientId);
        if (textbox1 != null)
            textbox1.value = txt;
        var textbox2 = obj.document.getElementById(valclientId);
        if (textbox2 != null)
            textbox2.value = val;

        if (fatherControls != "") {
            var fatherControls = setfatherID.split(",");
            var fatherTexts = fatherText.split(",");
            for (var i = 0; i < fatherControls.length; i++) {
                var father = obj.document.getElementById('ctl00_EmptyMasteCPH_' + fatherControls[i]);
                if (father != null) {
                    father.innerText = fatherTexts[i];
                }
            }
        }
        if (buttonId != "") {
            var button = obj.document.getElementById('ctl00_EmptyMasteCPH_' + buttonId);
            if (button != null)
                button.click();
        }


    }
    window.close();
}

function CheckAllForm(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];

        if (e.name.lastIndexOf('chbSelect') > 0) {
            e.checked = form.chkall.checked;
        }
    }
}

function remarkvalid(obj, len) {
    if (obj.value.length > len) {
        obj.value = obj.value.substr(0, len);
        ShowTopAlert("提示", "备注的长度不能大于" + len + "个字符");
        return false;
    }
    else {
        return true;
    }
}

function remarkvalid(obj, len, strMention) {
    var str = obj.value;
    var strlen = str.length;
    var charCode = -1;
    var realLength = 0;
    for (var i = 0; i < strlen; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) realLength += 1;
        else realLength += 2;

        if (realLength > len * 2) {
            obj.value = obj.value.substr(0, i);
            ShowTopAlert("提示", strMention + "的长度不能大于" + len * 2 + "个字符或" + len + "个汉字");
            return false;
        }
    }

    return true;
}

var getCoords = function(node) {
    var x = node.offsetLeft;
    var y = node.offsetTop;
    var parent = node.offsetParent;
    while (parent != null) {
        x += parent.offsetLeft;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return { x: x, y: y };
}
,
// 获取页面大小
pagesize = null,
getPageSize = function() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // Modify By Mudoo
    var visibleWidth, visibleHeight;
    visibleWidth = document.body.clientWidth;
    visibleHeight = document.body.clientHeight;
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, visibleWidth, visibleHeight);
    return arrayPageSize;
};
var telFomat = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/; //电话格式
//var mobileFomat=/^1[3,5]{1}[0-9]{1}[0-9]{8}$/; 
var mobileFomat = /^\d*$/; //不限制位?




//confirm�回调函数
function confirmCallback(control) {
    var obj = $("#" + control);
    if (obj.length > 0)
        obj[0].click();
}

//伸展和收?
//img:�点击触发伸展和收缩的img控件
// 伸展和收缩部分所在的容器（div,span,table 等）
function ShowOrHidden(img, panel) {

    if (document.getElementById(panel).style.display == "none")//由收缩设置为伸展 
    {
        document.getElementById(panel).style.display = "";
        document.getElementById(img).src = "/App_Themes/Common_CSS/images/Sub.gif"; //显示减号
        document.getElementById(img).alt = "隐藏";
        iframeAutoFit(); //iframe自动伸展，解决滚动问?

    }
    else  //�由伸展设置为收缩
    {
        document.getElementById(panel).style.display = "none";
        document.getElementById(img).src = "/App_Themes/Common_CSS/images/Add.gif"; //显示加号
        document.getElementById(img).alt = "显示";
    }
}

//判断是否有Radio被选择
function hasRadioCheck(scopeHtml) {
    var elements;
    if (scopeHtml == null) {

        elements = document.getElementsByTagName("input");

    }
    else {
        var curHtml = document.getElementById(scopeHtml);
        elements = curHtml.getElementsByTagName("input");
    }
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "radio" && elements[i].checked) {
            return true;
            break;
        }
    }
    return false;
}



//判断是否有Radio被选择
function hasCheckBoxCheck(scopeHtml) {
    var elements;
    if (scopeHtml == null) {

        elements = document.getElementsByTagName("input");

    }
    else {
        var curHtml = document.getElementById(scopeHtml);
        elements = curHtml.getElementsByTagName("input");
    }
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "checkbox" && elements[i].checked) {
            return true;
            break;
        }
    }
    return false;
}

//获得时间控件的时?
function GetTimeControlValue(control) {
    if (document.getElementById("txt" + control + "_cpDate") == null)
        return null;
    var date = document.getElementById("txt" + control + "_cpDate").value;
    if (date != "") {
        var hour = document.getElementById(control + "_ddlHour").value;
        var min = document.getElementById(control + "_ddlMinute").value;
        var time = new Date(Date.parse((date + " " + hour + ":" + min).replace(/-/g, "/")));
        return time;
    }
    else
        return null;
}


function GetTimeControlValue2(control) {
    if (document.getElementById(control + "_cpDate") == null)
        return null;
    var date = document.getElementById(control + "_cpDate").value;
    if (date != "") {
        var hour = document.getElementById(control + "_ddlHour").value;
        var min = document.getElementById(control + "_ddlMinute").value;
        var time = new Date(Date.parse((date + " " + hour + ":" + min).replace(/-/g, "/")));
        return time;
    }
    else
        return null;
}

function GetTimeControlValue2WithoutMinute(control) {
    if (document.getElementById(control + "_cpDate") == null)
        return null;
    var date = document.getElementById(control + "_cpDate").value;
    if (date != "") {
        var hour = document.getElementById(control + "_ddlHour").value;
        var time = new Date(Date.parse((date + " " + hour + ":00").replace(/-/g, "/")));
        return time;
    }
    else
        return null;
}

//�点击某个checkbox使某个HTML区块内的checkbox的全部选择和撤销
//function  SelectAll(oBox)
// {
//      var elements=$t("input",$(oBox));
//      for(var i=0;i<elements.length;i++)
//      {
//        if(elements[i].type=="checkbox")
//        {
//            if(elements[i].id!=event.srcElement.id)
//            {
//                elements[i].checked = event.srcElement.checked;
//                if (typeof (checkChange) != "undefined")
//                    checkChange(elements[i]);
//            }
//          }
//      }
// }
// 
//textBox整数输入的验?�如果不是数据清空textBox
function CheckInt(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /^\d*$/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
            CheckInt(txtBox);
        }
    }
}

//数字验证
function CheckNumber(value) {
    if (value != "") {
        var numFomat = /^(-)?\d*$/;
        if (!numFomat.test(value)) {
            return false;
        }
    }
    return true;
}

function CheckNumberDot(value) {
    if (value != "") {
        var numFomat = /^(-)?\d*(\.\d*)?$/;
        if (!numFomat.test(value)) {
            return false;
        }
    }
    return true;
}

//手机格式验证
function CheckMobile(value) {
    if (value != "") {
        if (!mobileFomat.test(value)) {
            return false;
        }
    }
    return true;
}

//电话格式验证
function CheckPhone(value) {
    if (value != "") {
        if (!telFomat.test(value)) {
            return false;
        }
    }
    return true;


}

//function CheckExttel(value) {
//    if (value != "") {
//        if (!/^\d*$/.test(value)) {
//            return false;
//        }
//    }
//    return true;
//}

function CheckEmail(value) {
    if (value != "") {
        if (!/^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*;)*$/.test(value)) {
            return false;
        }
    }
    return true;
}

//字符转换为半?�字符
function GetHalfWidthString(str) {
    var result = '';
    for (i = 0; i < str.length; i++) {
        code = str.charCodeAt(i);             //获取当前字符的unicode编码
        if (code >= 65281 && code <= 65373)   //unicode编码范围是所有的英文字母以及各种字符
        {
            result += String.fromCharCode(str.charCodeAt(i) - 65248);    //把全角字符的unicode编码转换为对应半角字符的unicode?
        }
        else if (code == 12288)                                      //�空格
        {
            result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32); //半角空格
        } else {
            result += str.charAt(i);                                     //原字符返?
        }
    }
    return result;
}


//�增加月份
function setKEEPENDDATE(str, p) {
    var s;
    var days = 0;
    var date = new Date(str.substring(0, 4), str.substring(5, 7) - 1, str.substring(8, 10));
    for (var i = 0; i < p; i++) {
        days = GetMonthDayCount(date);
        date.setDate(date.getDate() + days);
    }
    s = date.getYear() + "-";            //取年?
    s += date.getMonth() + 1 + "-"; //�取月?
    s += date.getDate();  //�取日?
    return s;
}

//   �返回某个日期对应的月份的天数
function GetMonthDayCount(date) {
    var m = date.getMonth() + 1;
    if (m == 12) {
        m = 1;
    } else {
        m += 1;
    }
    switch (m) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 4: case 6: case 9: case 11:
            return 30;
    }
    //feb:
    date = new Date(date);
    var lastd = 28;
    date.setDate(29);
    while (date.getMonth() == 1) {
        lastd++;
        AddDays(date, 1);
    }
    return lastd;
}

//   增加?
function AddDays(date, value) {
    date.setDate(date.getDate() + value);
}
function CheckTextBoxLength(obj, maxlength) {
    if (obj.value.length > maxlength) {
        obj.value = obj.value.substring(0, maxlength);
    }
}

//�给指定控件设置焦?
function setFocus(obj) {
    if (document.getElementById(obj) != null)
        document.getElementById(obj).focus();
}

//�计算两个日期之间相差的小时数
function getDateDiff(date1, date2) {
    var re = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
    var dt1, dt2;
    //if (re.test(date1)) {
    //dt1 = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3);
    dt1 = new Date(date1);
    //}

    if (re.test(date2)) {
        //dt2 = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3);
        dt2 = new Date(Date.parse((date2).replace(/-/g, "/")));
    }
    else {
        dt2 = new Date(date2);
    }
    return Math.floor((dt1 - dt2) / (1000 * 60 * 60))
}

function substr(ilength, obj) {
    var v = obj.value;
    if (v.length > ilength) {
        obj.value = v.substring(0, ilength);
    }
}

function CheckInt(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /(^\d*$)/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
        }
    }
}

//textBox整数输入的验?�如果不是数据清空textBox
function CheckNum(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
        }
    }
}

function CheckNumWithDot(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /(^-?[1-9]\d{0,9}(\.\d{0,6})?$)/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
        }
    }
}

function CheckNumWithDotZero(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /(^-?[0-9]\d{0,9}(\.\d{0,6})?$)/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
        }
    }
}

function CheckNumWithTwoDotZero(txtBox) {
    if (document.getElementById(txtBox) != null && document.getElementById(txtBox).value != "") {
        var numFormat = /(^-?[0-9]\d{0,9}(\.\d{0,2})?$)/;
        var text = document.getElementById(txtBox).value;
        if (!numFormat.test(text)) {
            document.getElementById(txtBox).value = text.substr(0, text.length - 1);
        }
    }
}


function DateAdd(interval, number, date) {
    /*
    *   功能:实现VBScript的DateAdd功能.
    *   参数:interval,字符串表达式，表示要添加的时间间?
    *   �参数:number,数值表达式，表示要添加的时间间隔的个数.
    *   参数:date,时间对象.
    *   返回:新的时间对象.
    *   var   now   =   new   Date();
    *   var   newDate   =   DateAdd( "d ",5,now);
    *---------------   DateAdd(interval,number,date)   -----------------
    */
    switch (interval) {
        case "y ":
            {
                date.setFullYear(date.getFullYear() + number);
                return date;
                break;
            }
        case "q ":
            {
                date.setMonth(date.getMonth() + number * 3);
                return date;
                break;
            }
        case "m ":
            {
                date.setMonth(date.getMonth() + number);
                return date;
                break;
            }
        case "w ":
            {
                date.setDate(date.getDate() + number * 7);
                return date;
                break;
            }
        case "d ":
            {
                date.setDate(date.getDate() + number);
                return date;
                break;
            }
        case "h ":
            {
                date.setHours(date.getHours() + number);
                return date;
                break;
            }
        case "m ":
            {
                date.setMinutes(date.getMinutes() + number);
                return date;
                break;
            }
        case "s ":
            {
                date.setSeconds(date.getSeconds() + number);
                return date;
                break;
            }
        default:
            {
                date.setDate(d.getDate() + number);
                return date;
                break;
            }
    }
}


function OnACEItemSelected(sender, args) {
    var obj = sender;
    var controlid = obj._id;
    var preid = "";
    if (controlid.indexOf("ctl00_EmptyMasteCPH_") >= 0) {
        preid = controlid.substr(0, controlid.indexOf("autoComplete1"));
    }
    else {
        preid = "ctl00_EmptyMasteCPH_" + controlid + "_";
    }
    $("#" + preid + "hidValue").val(args.get_value());
    $("#" + preid + "btnHID").click();

}

//function changetrclr(objtr) {
//    objtr.mouseover(function() {
//        $(this).addClass("tableOn").removeClass("tableOff");
//    }).mouseout(function() {
//        $(this).addClass("tableOff").removeClass("tableOn");
//    });
//})